﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZK.Models;
using ZK.Common;

namespace ZK.InterfaceServer
{
    public class CSApiServer
    {
        private static readonly string CsCN = "cs/";
        private static readonly string GetHardWareSAN = CsCN + "getHardWareS";//设备监控列表查询
        private static readonly string AddHardWareAN = CsCN + "addHardWare";//设备监控新增接口
        private static readonly string UpdateHardWareByidAN = CsCN + "updateHardWareByid";//设备监控列修改接口
        private static readonly string GetHardWareByIdAN = CsCN + "getHardWareById";//设备监控详情
        private static readonly string DelHardWareByIdAN = CsCN + "delHardWareById";//设备删除接口
        private static readonly string GetBlackAN = CsCN + "getBlack";//当前车辆是否为黑名单
        private static readonly string UpdateHardWareByNameAN = CsCN + "updateHardWareByName";//根据硬件名称修改硬件状态
        private static readonly string AutoControlTransportCN = "autoControlTransport/";
        private static readonly string AddHardWareLogAN = CsCN + "addHardWareLog";//根据硬件名称修改硬件状态
        private static readonly string TruckWeightBillAN = AutoControlTransportCN + "truckWeightBill";//生成磅单接口
        private static readonly string EndYxInfoAN = AutoControlTransportCN + "endYxInfo";//结束运销流程
        private static readonly string GetTheLatestAN = AutoControlTransportCN + "getTheLatest";//获取最后磅单信息接口


        /// <summary>
        /// 获取最后磅单信息接口
        /// </summary>
        /// <param name="cardNo"></param>
        /// <returns></returns>
        public static ReceiveDataBase<YxYsTransportReturnDto> GetTheLatest(string cardNo)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("cardNo", cardNo);
            string url = InterfaceExecute.GetUrl(GetTheLatestAN, keyValuePairs);
            string ret;
            try
            {
                ret = HttpClientUtil.doGetMethodToString(url);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("GetTheLatest " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ReceiveDataBase<YxYsTransportReturnDto>>(ret);

        }

        /// <summary>
        /// 结束运销流程
        /// </summary>
        /// <param name="cardNo"></param>
        /// <returns></returns>
        public static ReceiveDataBase<int?> EndYxInfo(string cardNo)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("cardNo", cardNo);
            string url = InterfaceExecute.GetUrl(EndYxInfoAN, keyValuePairs);
            string ret;
            try
            {
                ret = HttpClientUtil.doGetMethodToString(url);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("EndYxInfo " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ReceiveDataBase<int?>>(ret);

        }
        /// <summary>
        /// 生成磅单接口
        /// </summary>
        /// <param name="weightBillDtoSendM"></param>
        /// <returns></returns>
        public static ReceiveDataBase<int?> TruckWeightBill(WeightBillDtoSendM weightBillDtoSendM)
        {
            string url = InterfaceExecute.GetUrl(TruckWeightBillAN);
            string ret;
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url, Newtonsoft.Json.JsonConvert.SerializeObject(weightBillDtoSendM));
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("TruckWeightBill " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<ReceiveDataBase<int?>>(ret);

        }
        /// <summary>
        /// 添加硬件异常信息
        /// </summary>
        /// <param name="wareName"></param>
        /// <param name="wareState"></param>
        /// <param name="remark"></param>
        public static void AddHardWareLog(string wareName, int wareState, string remark)
        {
            if (Common.AppConfigHelper.GetAppSettings("HardWareLog") == "true")
            {
                var m = new { wareName = wareName, wareState = wareState, remark = remark };
                string ret;
                string url = InterfaceExecute.GetUrl(AddHardWareLogAN);
                try
                {
                    ret = HttpClientUtil.doPostMethodToString(url, Newtonsoft.Json.JsonConvert.SerializeObject(m));
                }
                catch (Exception ex)
                {
                    Common.Log4.LogManage.WriteErr("AddHardWareLog " + ex.Message);
                }
            }
        }

        /// <summary>
        /// 根据硬件名称修改硬件状态
        /// </summary>
        /// <param name="wareName"></param>
        /// <param name="wareState"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        //public static void UpdateHardWareByName(string wareName, int wareState, string remark)
        //{
        //    string ret;
        //    Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
        //    keyValuePairs.Add("wareName", wareName);
        //    keyValuePairs.Add("wareState", wareState.ToString());
        //    keyValuePairs.Add("remark", remark);
        //    string url = InterfaceExecute.GetUrl(UpdateHardWareByNameAN, keyValuePairs);
        //    try
        //    {
        //        ret = HttpClientUtil.doPostMethodToString(url);
        //    }
        //    catch (Exception ex)
        //    {
        //        Common.Log4.LogManage.WriteErr(ex.Message);
        //    }

        //}


        /// <summary>
        /// 根据硬件名称修改硬件状态
        /// </summary>
        /// <param name="wareName"></param>
        /// <param name="wareState"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<int?> UpdateHardWareByName(string wareName, int wareState, string remark)
        {
            string ret;
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("wareName", wareName);
            keyValuePairs.Add("wareState", wareState.ToString());
            keyValuePairs.Add("remark", remark);
            string url = InterfaceExecute.GetUrl(UpdateHardWareByNameAN, keyValuePairs);
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("UpdateHardWareByName " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<int?>>(ret);

        }


        /// <summary>
        /// 当前车辆是否为黑名单
        /// </summary>
        /// <param name="truckNo"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<bool> GetBlack(string truckNo)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("truckNo", truckNo);
            string url = InterfaceExecute.GetUrl(GetBlackAN, keyValuePairs);
            string ret;
            try
            {
                ret = HttpClientUtil.doGetMethodToString(url);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("GetBlack " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<bool>>(ret);

        }
        /// <summary>
        /// 设备删除接口
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<string> DelHardWareById(string hid)
        {
            string url = InterfaceExecute.GetUrl(DelHardWareByIdAN);
            string ret;
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url, hid);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("DelHardWareById " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<string>>(ret);

        }
        /// <summary>
        /// 设备监控列表查询
        /// </summary>
        /// <param name="getHardWareSSendM"></param>
        /// <returns></returns>
        public static GetHardWareSRceiveM GetHardWareS(GetHardWareSSendM getHardWareSSendM)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            string url = InterfaceExecute.GetUrl(GetHardWareSAN);
            string ret;
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url, Newtonsoft.Json.JsonConvert.SerializeObject(getHardWareSSendM));
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("GetHardWareS " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<GetHardWareSRceiveM>(ret);

        }
        /// <summary>
        /// 设备监控新增接口
        /// </summary>
        /// <param name="hardWareM"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<string> AddHardWare(HardWareM hardWareM)
        {
            string url = InterfaceExecute.GetUrl(AddHardWareAN);
            string ret;
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url, Newtonsoft.Json.JsonConvert.SerializeObject(hardWareM));
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("AddHardWare " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<string>>(ret);
        }
        /// <summary>
        /// 设备监控列更新接口
        /// </summary>
        /// <param name="hardWareM"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<string> UpdateHardWareByid(HardWareM hardWareM)
        {
            string url = InterfaceExecute.GetUrl(UpdateHardWareByidAN);
            string ret;
            try
            {
                ret = HttpClientUtil.doPostMethodToString(url, Newtonsoft.Json.JsonConvert.SerializeObject(hardWareM));
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("UpdateHardWareByid " + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<string>>(ret);

        }
        /// <summary>
        /// 设备监控详情
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        public static Models.ReceiveDataBase<HardWareM> GetHardWareById(string hid)
        {
            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("hid", hid);
            string url = InterfaceExecute.GetUrl(GetHardWareByIdAN, keyValuePairs);
            string ret;
            try
            {
                ret = HttpClientUtil.doGetMethodToString(url);
            }
            catch (Exception ex)
            {
                Common.Log4.LogManage.WriteErr("GetHardWareById" + ex.ToString());
                return null;
            }
            return Newtonsoft.Json.JsonConvert.DeserializeObject<Models.ReceiveDataBase<HardWareM>>(ret);

        }

    }
}
